<?php
/**
 * Created by PhpStorm.
 * User: ai
 * Date: 2018/1/9
 * Time: 10:07
 */

use yii\web\View;
use yii\helpers\Url;

use backend\assets\KartikAsset;
use kartik\popover\PopoverX;


$this->title = '上传案卷-智能阅卷 让阅卷更轻松';
$this->params['breadcrumbs'][] = $this->title;
$this->params['breadcrumbs'][] = ['label' => '我的案卷', 'url' => ['/yue-juan/my-upload'], 'target' => '_blank'];

/* @var $this yii\web\View */

KartikAsset::register($this);
$js = <<<JS
    $(function() {
        $("#input-folder").fileinput({
            browseLabel: '选择文件夹...',
            language:'zh',
            uploadAsync:true,//异步上传
            // maxFileCount:1, 
            // theme:'explorer',
            // previewFileType:'image',
            showBrowse:false,
            showCaption: false,
            dropZoneClickTitle:'（或点击选择要上传的文件夹）',
            // previewFileIcon: '<i class="fa fa-file"></i>',
            // allowedPreviewTypes: null, // set to empty, null or false to disable preview for all types
            allowedFileExtensions: ["jpg", "png", "jpeg", "pdf",],
            previewFileIconSettings: {
                'doc': '<i class="fa fa-file-word-o text-primary"></i>',
                'xls': '<i class="fa fa-file-excel-o text-success"></i>',
                'ppt': '<i class="fa fa-file-powerpoint-o text-danger"></i>',
                'jpg': '<i class="fa fa-file-photo-o text-warning"></i>',
                'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>',
                'zip': '<i class="fa fa-file-archive-o text-muted"></i>',
                'htm': '<i class="fa fa-file-code-o text-info"></i>',
                'txt': '<i class="fa fa-file-text-o text-info"></i>',
                'mov': '<i class="fa fa-file-movie-o text-warning"></i>',
                'mp3': '<i class="fa fa-file-audio-o text-warning"></i>',
            },
            previewFileExtSettings: {
                'doc': function(ext) {
                    return ext.match(/(doc|docx)$/i);
                },
                'xls': function(ext) {
                    return ext.match(/(xls|xlsx)$/i);
                },
                'ppt': function(ext) {
                    return ext.match(/(ppt|pptx)$/i);
                },
                'pdf': function(ext) {
                    return ext.match(/(pdf)$/i);
                },
                'jpg': function(ext) {
                    return ext.match(/(jp?g|png|gif|bmp)$/i);
                },
                'zip': function(ext) {
                    return ext.match(/(zip|rar|tar|gzip|gz|7z)$/i);
                },
                'htm': function(ext) {
                    return ext.match(/(php|js|css|htm|html)$/i);
                },
                'txt': function(ext) {
                    return ext.match(/(txt|ini|md)$/i);
                },
                'mov': function(ext) {
                    return ext.match(/(avi|mpg|mkv|mov|mp4|3gp|webm|wmv)$/i);
                },
                'mp3': function(ext) {
                    return ext.match(/(mp3|wav)$/i);
                },
            },
            browseOnZoneClick: true,
            showUpload: true, 
            showRemove: true, 
            showUploadedThumbs: true,
            uploadUrl: $("#uploadUrl").val(),
            deleteUrl: "",
            uploadExtraData:function (previewId, index) {
                var obj = {};
                obj['title']=$('#ocr_title').val()                                
                return obj;
            },
            slugCallback: function(filename) {
                // console.log(filename)
                return filename.replace('(', '_');
            },
        }).on('fileloaded', function(event, file, previewId, index, reader) {
            // console.log(file.webkitRelativePath);                       
            var relativePath=file.webkitRelativePath
            if(relativePath){
                var arr_path = relativePath.split('/')
                console.log(arr_path[0]);
                $('#ocr_title').val(arr_path[0])             
            } else if(file.name){
                $('#ocr_title').val(file.name)
            }else{
            console.log('no relative path');
            }            
        }).on('filebatchselected', function(event, files) {
            // console.log(files);
        })
                
                
          $("#images").fileinput({
            browseLabel: '选择文件夹...',
            language:'zh',
            uploadAsync:false,//同步上传
            // maxFileCount:1, 
            // theme:'explorer',
            // previewFileType:'image',
            showBrowse:false,
            showCaption: false,
            dropZoneClickTitle:'（或点击选择要上传的文件夹）',
            // previewFileIcon: '<i class="fa fa-file"></i>',
            // allowedPreviewTypes: null, // set to empty, null or false to disable preview for all types
            allowedFileExtensions: ["jpg", "png", "jpeg", "pdf",],
            previewFileIconSettings: {
                'jpg': '<i class="fa fa-file-photo-o text-warning"></i>',
                'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>',
            },
            previewFileExtSettings: {
                'jpg': function(ext) {
                    return ext.match(/(jp?g|png|gif|pdf)$/i);
                },
                 'pdf': function(ext) {
                    return ext.match(/(pdf)$/i);
                },
            },
            browseOnZoneClick: true,
            showUpload: true, 
            showRemove: true, 
            showUploadedThumbs: true,
            uploadUrl: $("#uploadUrlBatch").val(),
            deleteUrl: "",
            uploadExtraData:function (previewId, index) {
                var obj = {};
                obj['title']=$('#ocr_title_2').val()                
                return obj;
            },
            slugCallback: function(filename) {
                // console.log(filename)
                return filename.replace('(', '_');
            },
        }).on('fileloaded', function(event, file, previewId, index, reader) {
            // console.log(file.webkitRelativePath);
            var relativePath=file.webkitRelativePath
            if(relativePath){
                var arr_path = relativePath.split('/')
                // console.log(arr_path[0]);
                $('#ocr_title_2').val(arr_path[0])
            }else{
            console.log('no relative path');
            }            
        }).on('filebatchselected', function(event, files) {
            // console.log(files);
        })      
                
                
    });
JS;

$this->registerJs($js);
echo "<input id='uploadUrl' type='hidden' value='" . Url::to(['yue-juan/upload']) . "'>";
echo "<input id='uploadUrlBatch' type='hidden' value='" . Url::to(['yue-juan/upload-batch']) . "'>";

?>

<div class="row">
    <div class="col-sm-12">
        <label for="input-folder">通过文件夹上传文件</label>
        <div class="text-danger"><label>案卷名称：</label>
            <input type="text" name="title" id="ocr_title" class="title" placeholder="剥离卷一"
                   value="<?= Yii::$app->user->identity->realname . date('Y/m/d/His') ?>"></div>
        <div class="file-loading">
            <input id="input-folder" name="input-folder" type="file" multiple webkitdirectory accept="image/*">
        </div>
        <div id="errorBlock" class="help-block"></div>
    </div>
</div>
<hr>
<div class="row hidden">
    <div class="col-sm-12">
        <label for="input-folder">通过文件夹上传文件</label>
        <div class="text-danger"><label>案卷名称：</label>
            <input type="text" name="title" id="ocr_title_2" class="title" placeholder="剥离卷"
                   value="<?= Yii::$app->user->identity->realname . date('Y/m/d/His') ?>"></div>
        <div class="file-loading">
            <input id="images" name="images[]" type="file" multiple webkitdirectory accept="image/*">
        </div>
        <div id="errorBlock" class="help-block"></div>
    </div>
</div>

